<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 平台佣金
 * Class Controller_Admin_Finance_Commission_Index
 */
class Controller_Admin_Finance_Commission_Supplier extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
        //右侧导航
        $this->assign('parentkey', $this->params['parentkey']);
        $this->assign('prefix_url','finance/admin/finance/commission');
    }

    /**
     * @throws Kohana_Exception
     * 抽佣配置页
     */
    public function action_index()
    {
        $productlist = Model_Member_Order_Extend::get_commission_product();
        $list=ORM::factory('supplier_commission_config')->get_all();
        $info=array();
        foreach($list as $v)
        {
            $info[$v['varname']]=$v['value'];
        }
        $channel = Model_Supplier_Commission_Config::get_channel();

        $this->assign('channel',$channel);
        $this->assign('info',$info);
        $this->assign('productlist',$productlist);
        $this->display('admin/finance/commission/supplier_commission_type');
    }

    /**
     * 保存公告抽佣类型
     */
    public function action_ajax_config_commission_type_save()
    {
        $productlist = Model_Member_Order_Extend::get_commission_product();
        $channel = Model_Supplier_Commission_Config::get_channel();
        $config = array('type', 'ratio', 'cash');
        $varname_arr    = array();
        foreach($productlist as $p)
        {
            foreach($config as $c)
            {
                if($c=='type'){
                    $varname_arr[]  = "cfg_channel_commission_{$c}_".$p['id'];
                }else{
                    foreach ($channel as $v){
                        $varname_arr[]  = "cfg_channel_commission_{$c}_{$v['id']}_".$p['id'];
                    }
                }
            }
        }

        // 现金返佣时,佣金计算规则
        $varname_arr[] = 'cfg_commission_cash_calc_type';
        foreach($varname_arr as $varname)
        {
            $value = Arr::get($_POST, $varname);
            $value = intval($value);
            Model_Supplier_Commission_Config::save_normal_config($varname, $value);
        }
        echo json_encode(array('status' => true));
    }

    public function action_config_commission_product()
    {
        $action = $this->params['action'];
        $typeid = $this->params['typeid'];
        $menuid = $this->params['menuid'];
        if(empty($action)){
            $supplier_list = DB::query(Database::SELECT, "SELECT id,suppliername FROM sline_supplier WHERE find_in_set({$typeid},authorization)")->execute()->as_array();
            $productlist = Model_Member_Order_Extend::get_commission_product();
            $this->assign('typeid', $typeid);
            $this->assign('supplier_list', $supplier_list);
            $this->assign('products', $productlist);
            $this->assign('menuid', $menuid);
            $this->display('admin/finance/commission/supplier_commission_product');
        }
    }
}